package leetcode.code1004;

import leetcode.IDebug;
import leetcode.helper.HelpDebug;

//[0,0,1,1,1,0,0] 0 => 3
//[0,0,0,1] 4 => 4
public class Solution implements IDebug {

	public int longestOnes(int[] nums, int k) {
		int l = 0, r = 0;
		int len = nums.length;
		int zero = 0;
		while (r < len) {
			if (nums[r++] == 0) {
				zero++;
				if (zero >= k) {
					break;
				}
			}
		}
		// [0,0,1,1,1,0,0]
		// [0,0,0,1]
		int ans = 0;
		if (zero <= k) {
			ans = r;
		}
		while (r < len) {
			if (nums[r++] == 0) {
				zero++;
			}
			while (l < r && l < len && zero > k) {
				if (nums[l] == 0) {
					zero--;
				}
				l++;
			}
			if (zero == k) {
				ans = Math.max(ans, r - l);
			}
		}
		return ans;
	}

	@Override
	public void debug1() {
		HelpDebug.compare(6, this.longestOnes(HelpDebug.str2array3("[1,1,1,0,0,0,1,1,1,1,0]"), 2));

	}

	@Override
	public void debug2() {
		HelpDebug.compare(10, this.longestOnes(HelpDebug.str2array3("[0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1]"), 3));

	}

	@Override
	public void debug3() {
		HelpDebug.compare(3, this.longestOnes(HelpDebug.str2array3("[0,0,1,1,1,0,0]"), 0));

	}

	@Override
	public void debug4() {
		// TODO Auto-generated method stub
		HelpDebug.compare(4, this.longestOnes(HelpDebug.str2array3("[0,0,0,1]"), 4));

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
